<?php

	/**
	 * @author Jason Strese
	 * @copyright NGen Framework 2007-2008
	 * Core of the NGen Framework
	 */
	class NGenCore
	{
		/**
		 * Configurations
		 */
		public $configs = array( );
		/**
		 * Page (Smarty) object
		 */
		public $page = null;
		/**
		 * Database Driver [PGSQL, MySQL, SQLite]
		 */
		public $db = null;
		/**
		 * Session driver
		 */
		public $session = null;

		/**
		 * Constructor; requires main components
		 */
		public function __construct( &$page, &$session_driver, &$database_driver = null, $configs = array( ) )
		{
			
			$this->db = &$database_driver;
			$this->page = &$page;
			$this->configs = $configs;
			$this->session = &$session_driver;

			// Set a back reference to this core
			$this->page->core = &$this;
			$this->db->core = &$this;
						
			// Set our error handlers
			set_error_handler( 'error_handler_callback' );
			set_exception_handler( 'exception_handler_callback' );
		}
		
		/**
		 * @deprecated
		 * Handles the general errors that may occur
		 * * Kicks everything away, goes straight to general error template [g.error.tpl]
		 */
		public function general_error_handler($error)
		{
			$this->general_error_handler_callback('1', $error, '...', '1');
		}
		
		/**
		 * Identical to general_error_handler, regarding functionality anyways..
		 * This is just a valid callback to use as an error handler
		 */
		public function general_error_handler_callback($errno, $errstr, $errfile, $errline)
		{
			// Add the error to the configs for global reading
			$this->configs = array_merge( $this->configs, array( 'error' => array( 'message' => $errstr, 'num' => $errno, 'file' => isset( $errfile ) ? $errfile : '', 'line' => $errline ) ) );
			// Interrupt page handler
			$this->page->error = true;
			$this->page->getSection('error');
			$this->page->getAction('general');
			$this->page->load();
			exit;
		}
		
		/**
		 * Forms a common interface
		 * @since Feb 12th, 2008 
		 * @update Replaced $name(&$this, &$what) -> $name($this, $what)
		 */			
		public function getInterface( $name, $what = null )
		{
			if(!@include_once( './core/interfaces/'.$name.'.interface.php' ))
			{
				trigger_error('NGen::getInterface -> Call to a non-existant interface "'.$name.'"');
			}
			else
			{
				include_once( './core/interfaces/'.$name.'.interface.php' );
				return new $name($this, $what);
			}
		}
			
		public function __call($what, $args)
		{
			if( !isset($this->what) || $this->what === null)
			{
				trigger_error('NGenCore -> ['.$what.'] Requested driver cannot be null');
			}
		}
	}
?>